//TestScriptBlock.block
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(com.blocks.script);
importPackage(com.jme3.math);
importPackage(com.jme3.collision);
importPackage(com.jme3.scene.debug);
importPackage(com.jme3.scene);
importPackage(com.jme3.material);

var block = new Object();
var counter = 0;

var direction = 0;
var rayHit = false;
var mark;
block.setup = function(name) {
	thisBlock.setName("Direction");
	thisBlock.setTextureRow(1.0);
	thisBlock.setTextureColumn(1.0);
	thisBlock.resetTexture();
	//thisBlock.setCustomFrequency(true);
	thisBlock.getParameters().put("axis", 2);
	block.initMark(50);

}
block.loop = function(name, tpf) {
	var found = false;
	if (thisBlock.isPowered()) {		
	    var axis = new Float(thisBlock.getParameters().get("axis")).intValue();
	    switch (axis) {
		case 0:
			thisBlock.setColor(ColorRGBA.Red);
			break;
		case 1:
			thisBlock.setColor(ColorRGBA.Orange);
			break;
		case 2:
			thisBlock.setColor(ColorRGBA.Yellow);
			break;
		case 3:
			thisBlock.setColor(ColorRGBA.Green);
			break;			
		case 4:
			thisBlock.setColor(ColorRGBA.Blue);
			break;	
		default:
			thisBlock.setColor(ColorRGBA.Magenta);
			break;
		}
	    
	    masterBlock = ScriptBlocksApplication.getInstance().getMasterBlock(
				thisBlock.getMasterId());
		
		force = thisBlock.getParentFrequency(thisBlock.getBlockSides()) * tpf;
		
		moveto = Vector3f.ZERO;
		if (axis <= 2) {
			moveto = masterBlock.getLocalTranslation().add(
					(thisBlock.getLocalRotation().getRotationColumn(axis % 3)
							.mult(force)));
		} else {
			moveto = masterBlock.getLocalTranslation().subtract(
					(thisBlock.getLocalRotation().getRotationColumn(axis % 3)
							.mult(force)));
		}
		masterBlock.setLocalTranslation(moveto);
	}
	if (!found) {
		thisBlock.detachChild(mark);
		block.initMarkWhite();
		thisBlock.attachChild(mark);
	}
}

block.use = function() {
	var axis = new Float(thisBlock.getParameters().get("axis")).intValue(); 
	axis = (axis + 1) % 6;
	thisBlock.getParameters().put("axis", axis);
}

block.scriptExport = function() {
	return "";
}

block.initMark = function(distance) {
	if (mark != null) {
		ScriptBlocksApplication.getInstance().getRootNode().detachChild(mark);
	}
	var axis = thisBlock.getParameters().get("axis");
	var arrow = new Arrow(Vector3f.UNIT_X.mult(distance));
	if (axis == 1) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(distance));
	} else if (axis == 2) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(distance));
	} else if (axis == 3) {
		arrow = new Arrow(Vector3f.UNIT_X.mult(-distance));
	} else if (axis == 4) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(-distance));
	} else if (axis == 5) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(-distance));
	}

	arrow.setLineWidth(3);

	mark = new Geometry("BOOM!", arrow);

	mark_mat = new Material(ScriptBlocksApplication.getInstance()
			.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
	mark_mat.setColor("Color", ColorRGBA.Red);
	mark.setMaterial(mark_mat);
}
block.initMarkWhite = function() {
	if (mark != null) {
		ScriptBlocksApplication.getInstance().getRootNode().detachChild(mark);
	}
	var axis = thisBlock.getParameters().get("axis");
	var arrow = new Arrow(Vector3f.UNIT_X.mult(2));
	if (axis == 1) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(2));
	} else if (axis == 2) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(2));
	} else if (axis == 3) {
		arrow = new Arrow(Vector3f.UNIT_X.mult(-2));
	} else if (axis == 4) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(-2));
	} else if (axis == 5) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(-2));
	}
	arrow.setLineWidth(3);

	mark = new Geometry("BOOM!", arrow);

	mark_mat = new Material(ScriptBlocksApplication.getInstance()
			.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
	mark_mat.setColor("Color", ColorRGBA.White);
	mark.setMaterial(mark_mat);
}
